#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#

import celtuce.help.logging as logging

import threading
import Queue as queue
import time

class   Handler(threading.Thread) :
    def __init__(self, event_queue, handle_callable, poll_timeout=10) :
        threading.Thread.__init__(self)

        self._event_queue = event_queue
        self._handle_callable = handle_callable
        self._poll_timeout = poll_timeout

        self._would_stop = False
        self._logger = logging.getLogger('celtuce')
        ## __init__()

    def run(self) :
        while not self._would_stop :
            try :
                request = self._event_queue.get(True, self._poll_timeout)
            except queue.Empty :
                continue

            self._handle_callable(request)
            self._event_queue.task_done()
            continue

            #try :
            #    self._handle_callable(request)
            #except :
            #    # just ignore any exception
            #    self._logger.excpt()
            #    continue
        ## run()

    def stop(self) :
        self._would_stop = True
        ## stop()

    ## class Handler


class   Celler(object) :
    #def __new__(cls) :
    #    self = object.__new__(cls)
    #    self.event_queue = queue.Queue(0)
    #    self._handler = Handler(self.event_queue, self.handle)
    #    return self
    #    ## __new__()

    def __init__(self) :
        self._id_base = id(self)
        self._id_serial = 1 
        self._id_maximum = 1024 * 1024 * 1024

        self.event_queue = queue.Queue(0)
        self._handler = Handler(self.event_queue, self.handle)
        self._logger = logging.getLogger('celtuce')
        ## __init__()

    def handle(self, event_request) :
        '''handle request event, and must be overload

        @param event_request Event object of request

        @return None
        '''
        pass
        ## handle()

    def start(self) :
        self._handler.start()
        ## start()

    def stop(self) :
        #self._logger.debug('stop handler (%s) ...', str(self._handler))
        self._handler.stop()
        if self._handler.isAlive() :
            self._handler.join()
        ## stop()

    def getEventQueue(self) :
        return  self.event_queue
        ## getEventQueue()

    def genRequestID(self) :
        # TODO: lock
        serial = self._id_serial

        self._id_serial += 1
        if self._id_serial >= self._id_maximum :
            self._id_serial = 1

        id = '.'.join((str(self._id_base), str(serial)))
        return id
        ## genRequestID()

    ## class Celler


